//
//  CTUserPublishTableListView.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/5/25.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTUserPublishTableListView.h"
#import "CTDebugManager.h"
#import "CTHeader.h"
#import "CTUserPublishTableCell.h"
#import "CTDiscoveryItems.h"
#import "CTDeviceFunction.h"
#import "CTDataGenerator.h"
#import "RefreshControl.h"
#import "CTFollowCell.h"
#import "CTUser.h"
#import "CTSegmentView.h"
#import "CTFile.h"
#import "CTTextManager.h"

@interface CTUserPublishTableListView()<RefreshControlDelegate,UITableViewDataSource,UITableViewDelegate,CTUserPublishTableCellDelegate,CTFllowTableCellDelegate>
{
    UITableView *pubTableView;
    NSMutableArray * pubDataMutableArray;
    NSMutableArray * recommendListMutableArray;
    
    RefreshControl * refreshControl;
}

@end

@implementation CTUserPublishTableListView
@synthesize delegate;
- (void)dealloc
{
    [pubTableView removeFromSuperview];
    pubTableView = nil;
    delegate = nil;
    
    recommendListMutableArray = nil;
    
    refreshControl = nil;
    
    pubDataMutableArray = nil;
    [CTDebugManager showErrorLogInfo:@"dealloc : CTUserFavoriteList Is Removed!!!!"];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
      //  recommendListMutableArray = [[CTDataGenerator generateUserSuggestionItems] mutableCopy];
        self.backgroundColor = [UIColor clearColor];
        [self createUserFavoriteListTableView];
    }
    return self;
}
- (void)createUserFavoriteListTableView
{
    pubTableView = [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
    pubTableView.delegate = self;
    pubTableView.dataSource = self;
    pubTableView.showsVerticalScrollIndicator = YES;
    pubTableView.backgroundColor = [UIColor clearColor];
    pubTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    pubTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    [pubTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self addSubview:pubTableView];
    
    
    /*
     *** refresh control
     */
    refreshControl = [[RefreshControl alloc] initWithScrollView:pubTableView delegate:self];
    refreshControl.topEnabled = YES;
    refreshControl.bottomEnabled = NO;
    refreshControl.enableInsetBottom = 60;
    
}
#pragma mark-UITableViewDataSource
//分区个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0)
    {
        if(recommendListMutableArray.count >0)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    if (section == 1)
    {
        return pubDataMutableArray.count;
    }

    return 0;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger section = indexPath.section;
    if (section == 0) {
       
        static NSString *cellIdentifier = @"Cell_Follow";
        CTFllowTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[CTFllowTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            cell.delegate = self;
        }
        cell.item = [recommendListMutableArray objectAtIndex:indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        pubTableView.rowHeight = 100;
        
        return cell;
        
    }else{
        static NSString *cellIdentifier = @"Cell";
        CTUserPublishTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[CTUserPublishTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            cell.delegate = self;
        }

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        CTFollowList * item = [pubDataMutableArray objectAtIndex:indexPath.row];
        cell.cellItem = item;
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{

    if (indexPath.section ==0)
    {
        
    }
}



#pragma mark - action & delegate -
-(void)refresh
{
    [refreshControl startRefreshingDirection:RefreshDirectionTop];
}

-(void)refreshPullDownEnd
{
    [refreshControl finishRefreshingDirection:RefreshDirectionTop];
}

-(void)refreshPullUpEnd
{
    [refreshControl finishRefreshingDirection:RefreshDirectionBottom];
}

-(void)reloadData
{
    [pubTableView reloadData];
}

-(void)setHeadView:(UIView *)headView
{
    CTSegmentView * segView = (CTSegmentView *)[headView viewWithTag:10000];
    
    NSArray * subViewsArray = [segView subviews];
    for (UIButton * btn in subViewsArray) {
        
        if (btn.tag == 1) {
            [btn setBackgroundImage:[UIImage imageNamed:@"liebiao"] forState:UIControlStateNormal];
        }else if (btn.tag == 2)
        {
            [btn setBackgroundImage:[UIImage imageNamed:@"gongge"] forState:UIControlStateNormal];
        }else if (btn.tag == 4)
        {
            [btn setBackgroundImage:[UIImage imageNamed:@"zhuanti"] forState:UIControlStateNormal];
        }
        
    }
    
    UIButton * btn = (UIButton *)[segView viewWithTag:1];
    [btn setBackgroundImage:[UIImage imageNamed:@"liebiao_click"] forState:UIControlStateNormal];
    
    UIImageView * segLineView = (UIImageView *)[segView viewWithTag:1000];
    
    CGRect rect = segLineView.frame;
    rect.origin.x = btn.frame.origin.x -(segLineView.frame.size.width - btn.frame.size.width)/2.0;
    segLineView.frame = rect;
    
    pubTableView.tableHeaderView = headView;
    [pubTableView reloadData];
}





#pragma mark - refresh control delegate -

- (void)refreshControl:(RefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection) direction
{
    if (direction == RefreshDirectionTop) {
        if ([self.delegate respondsToSelector:@selector(refreshControlPullDown:)]) {
            [self.delegate refreshControlPullDown:self];
        }
        
    }else{
        if ([self.delegate respondsToSelector:@selector(refreshControlPullUp:)]) {
            [self.delegate refreshControlPullUp:self];
        }
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if ([self.delegate respondsToSelector:@selector(userPublishTableListScrollViewDidScroll:)]) {
        [self.delegate userPublishTableListScrollViewDidScroll:scrollView];
    }
    
    if (scrollView.contentOffset.y >= 0.0 && scrollView.contentOffset.y < 80) {
        [refreshControl finishRefreshingDirection:RefreshDirectionTop];
    }
    if (scrollView.contentOffset.y + scrollView.frame.size.height<= scrollView.contentSize.height && scrollView.contentOffset.y + scrollView.frame.size.height >= scrollView.contentSize.height - 80) {
        [refreshControl finishRefreshingDirection:RefreshDirectionBottom];
    }
    
    if (scrollView.contentOffset.y + scrollView.frame.size.height <= scrollView.contentSize.height+50) {
        refreshControl.isPullDown = YES;
    }else{
        refreshControl.isPullDown = NO;
    }
    
}



#pragma mark tableViewCellDelegate
/**
 *@brief 点击评论总数
 */
- (void)userPublishTableCellCommentCountAction:(CTUserPublishTableCell *)cell withItem:(CTFollowList *)item
{
    if ([self.delegate respondsToSelector:@selector(userPublishTableListCommentCountAction:withItem:)])
    {
        [self.delegate userPublishTableListCommentCountAction:self withItem:item];
    }
}
/**
 *@brief 点击评论第一条
 */
- (void)userPublishTableCellCommentHitFirstLabelAction:(CTUserPublishTableCell *) cell withItem:(CTFollowList *)item
{
    if ([self.delegate respondsToSelector:@selector(userPublishTableListCommentHitFirstLabelAction:withItem:)])
    {
        [self.delegate userPublishTableListCommentHitFirstLabelAction:self withItem:item];
    }
}
/**
 *@brief 点击评论第二条
 */
- (void)userPublishTableCellCommentHitSecondLabelAction:(CTUserPublishTableCell *) cell withItem:(CTFollowList *)item
{
    if ([self.delegate respondsToSelector:@selector(userPublishTableListCommentHitSecondLabelAction:withItem:)])
    {
        [self.delegate userPublishTableListCommentHitSecondLabelAction:self withItem:item];
    }
}
/**
 *@brief 点击评论第一条头像
 */
- (void)userPublishTableCellCommentHitFirstHeadAction:(CTUserPublishTableCell *)cell withItem:(CTComment *) item
{
    if ([self.delegate respondsToSelector:@selector(userPublishTableListCommentHitFirstHeadAction:withItem:)])
    {
        [self.delegate userPublishTableListCommentHitFirstHeadAction:self withItem:item];
    }
}
/**
 *@brief 点击评论第二条头像
 */
- (void)userPublishTableCellCommentHitSecondHeadAction:(CTUserPublishTableCell *)cell withItem:(CTComment *)item
{
    if ([self.delegate respondsToSelector:@selector(userPublishTableListCommentHitSecondHeadAction:withItem:)])
    {
        [self.delegate userPublishTableListCommentHitSecondHeadAction:self withItem:item];
    }
}
/**
 *@brief 点击头像
 */
- (void)userPublishTableCellHitHeadAction:(CTUserPublishTableCell *)cell withItem:(CTFollowList *)item
{
    if ([self.delegate respondsToSelector:@selector(userPublishTableListHitHeadAction:withItem:)])
    {
        [self.delegate userPublishTableListHitHeadAction:self withItem:item];
    }
}
/**
 *@brief 点击大图
 */
- (void)userPublishTableCellHitPicImageBtnAction:(CTUserPublishTableCell *)cell withItem:(CTFollowList *)item withImageView:(UIImageView *)picimageView
{
    if ([self.delegate respondsToSelector:@selector(userPublishTableListHitPicImageBtnAction:withItem:withImageView:)])
    {
        [self.delegate userPublishTableListHitPicImageBtnAction:self withItem:item withImageView:picimageView];
    }
}
/**
 *@brief 点赞
 */
- (void)userPublishTableCellHitFavourBtnAction:(CTUserPublishTableCell *)cell withItem:(CTFollowList *)item withBool:(BOOL) favBool
{
    CTListDetailUser *listItme = [[CTListDetailUser alloc]init];
    listItme.profile_image_url_thumb = [CTUser userInfo].profile_image_url_thumb;
    listItme.name = [CTUser userInfo].name;
    listItme.gender = [CTUser userInfo].gender;
    listItme.uid = [CTUser userInfo].uid;
    if (favBool == YES)
    {
        item.liked = @"1";
        NSMutableArray * itemArray =  [item.like_list mutableCopy];
        [itemArray addObject:listItme];
        item.like_list = nil;
        item.like_list = itemArray;
        itemArray = nil;
        if (item.like_list.count == 1)
        {
            [pubTableView reloadData];
        }
    }
    else
    {
        item.liked = @"0";
        NSMutableArray * itemArray = [item.like_list mutableCopy];
        for (int i = 0; i <itemArray.count; i++)
        {
            CTListDetailUser *userItem = [itemArray objectAtIndex:i];
            if ([userItem.uid isEqualToString:listItme.uid])
            {
                if (itemArray.count ==1)
                {
                    [itemArray removeObjectAtIndex:i];
                    [pubTableView reloadData];
                }
                else
                {
                    [itemArray removeObjectAtIndex:i];
                }
                break;
            }
        }
        item.like_list = nil;
        item.like_list = itemArray;
    }

    if ([self.delegate respondsToSelector:@selector(userPublishTableListHitFavBtnHeadAction:withItem:)])
    {
        [self.delegate userPublishTableListHitFavourBtnAction:self withItem:item withBool:favBool];
    }
}
/**
 *@brief 点击评论按钮
 */
- (void)userPublishTableCellHitCommentBtnaction:(CTUserPublishTableCell *) cell withItem:(CTFollowList *)item
{
    if ([self.delegate respondsToSelector:@selector(userPublishTableListHitCommentBtnaction:withItem:)]) {
        [self.delegate userPublishTableListHitCommentBtnaction:self withItem:item];
    }
}
/**
 *@brief 点击分享按钮
 */
- (void)userPublishTableCellHitShareBtnAction:(CTUserPublishTableCell *) cell withItem:(CTFollowList *)item withImage:(UIImage *)img
{
    if ([self.delegate respondsToSelector:@selector(userPublishTableListHitShareBtnAction:withItem:withImage:)])
    {
        [self.delegate userPublishTableListHitShareBtnAction:self withItem:item withImage:img];
    }
}
/**
 *@brief 点击更多按钮
 */
- (void)userPublishTableCellHitMoreBtnAction:(CTUserPublishTableCell *) cell withItem:(CTFollowList *)item
{
    if ([self.delegate respondsToSelector:@selector(userPublishTableListHitMoreBtnAction:withItem:)]) {
        [self.delegate userPublishTableListHitMoreBtnAction:self withItem:item];
    }
}
/**
 *@brief 点击点赞的人的头像
 */
- (void)userPublishTableCellHitFavBtnHeadAction:(CTUserPublishTableCell *)cell withItem:(CTListDetailUser *)item
{
    if ([self.delegate respondsToSelector:@selector(userPublishTableListHitFavBtnHeadAction:withItem:)])
    {
        [self.delegate userPublishTableListHitFavBtnHeadAction:self withItem:item];
    }
}
/**
 *@brief 点击关注按钮
 */
- (void)userPublishTableCellHitAttentionBtnAction:(CTUserPublishTableCell *) cell withItem:(CTFollowList *)item withBool:(BOOL)attentionBool
{
    if (attentionBool == YES)
    {
        item.user.following = @"1";
    }
    else
    {
        item.user.following = @"0";
    }

    if ([self.delegate respondsToSelector:@selector(userPublishTableListHitAttentionBtnAction:withItem:withBool:)])
    {
        [self.delegate userPublishTableListHitAttentionBtnAction:self withItem:item withBool:attentionBool];
    }
}
/**
 *@brief 点击话题
 */
- (void)userPublishTableCellHitTopicBtnAction:(CTUserPublishTableCell *) cell withItem:(CTTopic *)item
{
    if ([self.delegate respondsToSelector:@selector(userPublishTableListHitTopicBtnAction:withItem:)])
    {
        [self.delegate userPublishTableListHitTopicBtnAction:self withItem:item];
    }
}


#pragma mark followTableCellDelegate
-(void)followTableCellHitAttentionBtnAction:(CTFllowTableCell *)cell withItme:(CTUserSuggestionItem *)item
{
    if ([self.delegate respondsToSelector:@selector(userPublishTableListRecommendHitAttentionBtnAction:withItem:)])
    {
        [self.delegate userPublishTableListRecommendHitAttentionBtnAction:self withItem:item];
    }
}

-(void)followTableCellHitHeadAction:(CTFllowTableCell *)cell withItem:(CTUserSuggestionItem *)item
{
    if ([self.delegate respondsToSelector:@selector(userPublishTableListRecommendHitHeadAction:withItem:)])
    {
        [self.delegate userPublishTableListRecommendHitHeadAction:self withItem:item];
    }
}





#pragma mark - setter -
-(void)setFavDataArray:(NSArray *)pubDataArray
{
    
    @try {
        NSMutableDictionary * infoDic = [CTFile getDicFile:@"followList.plist" fromDoc:CTFileDocLibrary fromPath:@"cacheData/discovery"];
       
        if ([[NSString stringWithFormat:@"%@",[infoDic objectForKey:@"next_page"]] isEqualToString:@"1"])refreshControl.bottomEnabled = YES;
        else refreshControl.bottomEnabled = NO;
    }
    @catch (NSException *exception) {
        refreshControl.bottomEnabled = NO;
    }
    
    pubDataMutableArray = [pubDataArray mutableCopy];
    [pubTableView reloadData];
}

-(void)setUserFavDataArray:(NSArray *)pubUserDataArray
{
    @try {
        NSMutableDictionary * infoDic = [CTFile getDicFile:@"userPublishDetailList.plist" fromDoc:CTFileDocLibrary fromPath:@"cacheData/user"];
        
        if ([[NSString stringWithFormat:@"%@",[infoDic objectForKey:@"next_page"]] isEqualToString:@"1"])refreshControl.bottomEnabled = YES;
        else refreshControl.bottomEnabled = NO;
    }
    @catch (NSException *exception) {
        refreshControl.bottomEnabled = NO;
    }
    
    pubDataMutableArray = [pubUserDataArray mutableCopy];
    [pubTableView reloadData];
}

-(void)setUserRecommendDataArray:(NSArray *)recommendListArray
{
    recommendListMutableArray = [recommendListArray mutableCopy];
    [pubTableView reloadData];
}

-(void)setTableViewScroll:(BOOL)scrollEnabled
{
    pubTableView.scrollEnabled = scrollEnabled;
}

@end
